module diagnostics

    use time_manager
    use dynamic_quantities
    use dist_recon

    character(20) :: output_diag_file
    logical :: check_conservation

    namelist /diagnostics_parameters/ &
        output_diag_file, check_conservation

contains

    subroutine diagnostics_init(namelist_file)
        character(*), intent(in) :: namelist_file

        check_conservation = .false.

        open(10, file=namelist_file)
        read(10, nml=diagnostics_parameters, end=10)
10      close(10)

        return
    end subroutine diagnostics_init

    subroutine diagnostics_main(h_index, u_index)
        integer, intent(in) :: h_index, u_index(num_dim)

        real :: total_mass, total_momentum(num_dim)
        integer :: i

        if(cur_time == 0) then
            open(11, file=output_diag_file)
        else
            open(11, file=output_diag_file, position="append")
        end if

        if(check_conservation) then
            call dist_recon_integrate(op_s, total_mass, h_index)
            do i = 1, num_dim
                call dist_recon_integrate(op_m, total_momentum(i), h_index, u_index(i))
            end do
        end if

        if(check_conservation) then
            write(11, "(2f)", advance="no") total_mass, total_momentum
        end if
        write(11, *)

        close(11)

        return
    end subroutine diagnostics_main

end module diagnostics
